import { ref } from 'vue'
import { defineStore } from 'pinia'
import { Login, Register, Logout } from '@/api/account'

export const useAppStore = defineStore('app', () => {
    const collapse = ref(false)
    const username = ref('')
    const token = ref('')
<<<<<<< HEAD
=======
    const avatar = ref('')
    const role = ref('')
    const userId = ref('')
    const enterpriseId = ref('')
>>>>>>> resume

    function loginAction(requestData) {
        return new Promise((resolve, reject) => {
            Login(requestData)
                .then((response) => {
                    const data = response.data
                    updateUsername(data.username)
                    updateToken(data.token)
<<<<<<< HEAD
=======
                    updateAvatar(data.avatar || '')
                    updateRole(data.role || 'user')
                    updateUserId(data.id || '')
                    updateEnterpriseId(data.enterpriseId || '')
>>>>>>> resume
                    resolve(response)
                })
                .catch(error => {
                    reject(error)
                })
        })
    }

    function logoutAction(requestData) {
        return new Promise((resolve, reject) => {
            Logout(requestData)
                .then((response) => {
                    updateUsername('')
                    updateToken('')
<<<<<<< HEAD
                    resolve(response)
                })
                .catch(error => {
=======
                    updateAvatar('')
                    updateRole('')
                    updateUserId('')
                    updateEnterpriseId('')
                    resolve(response)
                })
                .catch(error => {
                    // 即使接口失败也要清除本地数据
                    updateUsername('')
                    updateToken('')
                    updateAvatar('')
                    updateRole('')
                    updateUserId('')
                    updateEnterpriseId('')
>>>>>>> resume
                    reject(error)
                })
        })
    }
    
    function registerAction(requestData) {
        return new Promise((resolve, reject) => {
            Register(requestData)
                .then((response) => {
                    resolve(response)
                })
                .catch(error => {
                    reject(error)
                })
        })
    }

    function toggleCollapseAction() {
        collapse.value = !collapse.value
    }

    function updateUsername(value) {
        username.value = value
    }

    function updateToken(value) {
        token.value = value
    }

<<<<<<< HEAD
    return { collapse, username, token, loginAction, registerAction, logoutAction, toggleCollapseAction, updateToken }
}, {
    persist: {
        key: 'pinia.app',
        storage: sessionStorage,
=======
    function updateAvatar(value) {
        avatar.value = value
    }

    function updateRole(value) {
        role.value = value
    }

    function updateUserId(value) {
        userId.value = value
    }

    function updateEnterpriseId(value) {
        enterpriseId.value = value
    }

    return { collapse, username, token, avatar, role, userId, enterpriseId, loginAction, registerAction, logoutAction, toggleCollapseAction, updateToken, updateAvatar, updateRole, updateUserId, updateEnterpriseId }
}, {
    persist: {
        key: 'pinia.app',
        storage: localStorage,
>>>>>>> resume
        // paths: ['collapse'],
    }
})